<?php
/**
 * Description of articles
 *
 * @author:	qfox
 * @package:	radiance
 * @copyright:	(c) 2011, alex <qfox@ya.ru> yaroshevich aka alex_ez. All rights reserved
 * @created:	11.05.2011, 14:34
 */
class articles_admin_controller extends refbook_controller {

	protected $sizes = array( 'a' => '728,90', 'b' => '480,60', 'c' => '240,60' );
	protected $states = array( );

	protected $entity_title = 'article';
	protected $entities_title = 'articles';
	protected $captions = null;
	protected $item_title_key = 'title';

	// form and filter
	protected $pager_per_pages = '20,50,200,500';

	protected $order = 'id-';

	/**
	 * assets
	 * @var array
	 */
	protected $assets = array(
		'js' => 'admin/articles',
		);

	protected $url_patterns = array(
		'index' => '/admin/articles/',
		'show' => '/admin/articles/id:id/',
		'add' => '/admin/articles/add/',
		'edit' => '/admin/articles/edit/?id=:id',
		);

	public function on_call( &$walker ) {
		if (!$this->conf['enabled'])
			respi()->notfound();
	}

	public function on_initialize( &$walker ) {
		$this->conf = $this->config->articles;
		$this->conf['enabled'] = isset($this->conf['enabled']) && $this->conf['enabled'];
		if (!$this->conf['enabled'])
			return;

		$this->model = new articles();
		$this->states = $this->model->get_states();
		$this->types = $this->model->get_types();

		$this->groups = new articles_groups();
		$this->authors = new articles_authors();
		
		$this->columns = array(
			'id' => array(
				'text' => __('ID'),
				'width' => '30px',
				'order' => true,
				),
			'tm' => array(
				'text' => __('Creation date'),
				'order' => true,
				'format' => 'datetime',
				),
			'date' => array(
				'text' => __('Publication date'),
				'order' => true,
				'format' => 'datetime',
				),
			'state' => array(
				'text' => __('State'),
				'render' => true,
				),
			'group' => array(
				'text' => __('Group'),
				'order' => '(select `title` from core_articles_groups where `id` = `group_id`)+,`id`-',
				'value' => '(select `title` from core_articles_groups where `id` = `group_id`)',
				),
			'author' => array(
				'text' => __('Author'),
				'order' => '(select `title` from core_articles_authors where `id` = `author_id`)+,`id`-',
				'value' => '(select `title` from core_articles_authors where `id` = `author_id`)',
				),
			'title' => array(
				'text' => __('Title'),
				'order' => true,
				),
			'type' => array(
				'text' => __('Type'),
				'render' => true,
				),
			'links' => array(
				'text' => __('Links quantity'),
				'order' => '(select count(`id`) as `links` from core_articles_links as `l` where `core_articles`.`id` = `l`.`article_id` and `l`.`state` = "on")+,`id`-',
				'value' => '(select count(`id`) as `links` from core_articles_links as `l` where `core_articles`.`id` = `l`.`article_id` and `l`.`state` = "on")',
				),
			'looks' => array(
				'text' => __('Looks'),
				'order' => true,
				),
			);

		$this->captions = array(
			'title'		=> __('Articles'),
			'index'		=> __('Articles list'),

			'show'		=> __('Show article'),
			'add'		=> __('Create article'),
			'edit'		=> __('Modify article'),
			'remove'	=> __('Delete article'),

			'showing'	=> __('Article "%s"'),
			'adding'	=> __('New article'),
			'editing'	=> __('Modifing article "%s"'),
			'removing'	=> __('Deleting article "%s"'),
			);

		$this->fieldsets['index#filter'] = array(
			'id' => array( 'label' => __('ID'), 'type' => 'text', 'class' => 'shorttext' ),
			'tm' => array( 'label' => __('Creation date'), 'type' => 'date_period' ),
			'date' => array( 'label' => __('Publication date'), 'type' => 'date_period' ),
			'state' => array( 'label' => __('State'), 'type' => 'select', 'list' => ':states', ),
			'type' => array( 'label' => __('Type'), 'type' => 'select', 'list' => ':types', ),
			'group_id' => array( 'label' => __('Group'), 'type' => 'select', 'list' => ':groups', ),
			'author_id' => array( 'label' => __('Author'), 'type' => 'select', 'list' => ':authors', ),
			'title' => array( 'label' => __('Title'), 'type' => 'text' ),
			);

		if (!$walker->selfonly)
			parent::on_initialize( $walker );
	}

	protected function init_crumbs() {
		parent::init_crumbs();
	}
	
	public function init_lists() {
		$this->lists['types'] = $this->types;
		$this->lists['states'] = $this->states;
		$this->lists['groups'] = $this->groups->by();
		$this->lists['authors'] = $this->authors->by();
	}

	static public function get_menu_items( ) {
		if (!@config::instance()->articles['enabled'])
			return;
		$r = array(
			'-' => array( 'text' => '-', 'section' => 'content', 'order' => 1500 ),
			'articles' => array( 'text' => __('Articles') ),
			'articles/authors' => array( 'text' => __('Articles authors') ),
			'articles/groups' => array( 'text' => __('Articles groups') ),
			);
		return $r;
	}

	protected function get_item( $id ) {
		return parent::get_item( $id );
	}

	public function render_field_state($k,$v) {
		return @$this->lists['states'][$v['state']];
	}

	public function render_field_type($k,$v) {
		return @$this->lists['types'][$v['type']];
	}

}


